package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.ApplyRestaurant;
import com.wm.bean.UserScriptorium;
import com.wm.comm.Constant;
import com.wm.dao.ApplyRestaurantDAO;
import com.wm.dao.UserScriptoriumDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class ApplyRestaurantHibernateDAO extends HibernateDaoSupport implements ApplyRestaurantDAO {
	
	private static final String LOAD_BY_ID = "from ApplyRestaurant where id = ?";
	private static final String  REMOVE_BY_ID = "delete ApplyRestaurant where id = ?";
	
	
	/**
	 * 保存ApplyRestaurant对象
	 * 
	 * @param ar
	 * 			ApplyRestaurant
	 * @return  ApplyRestaurant
	 * 			ApplyRestaurant
	 */
	public ApplyRestaurant saveApplyRestaurant(ApplyRestaurant ar){
		this.getHibernateTemplate().save(ar);
		return ar;
	}
	
	/**
	 * 更新ApplyRestaurant对象
	 * 
	 * @param ar
	 * 			ApplyRestaurant
	 * @return  ApplyRestaurant
	 * 			ApplyRestaurant
	 */
	public ApplyRestaurant updateApplyRestaurant(ApplyRestaurant ar){
		this.getHibernateTemplate().update(ar);
		return ar;
	}
	
	/**
	 * 根据ID删除ApplyRestaurant对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeApplyRestaurantById(final long id){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	/**
	 * 根据ID获取ApplyRestaurant对象
	 * 
	 * @param id
	 * 			long
	 * @return  ApplyRestaurant
	 * 			ApplyRestaurant
	 */
	public ApplyRestaurant findApplyRestaurantById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (ApplyRestaurant) l.get(0);
		}
	}
	
	/**
	 * 获取ApplyRestaurant对象个数
	 * 
	 * @param applyType
	 * 			int
	 * @return
	 */
	public long getApplyRestaurantNum(int applyType){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(id) from ApplyRestaurant where 1=1");
		if (applyType != -1) {
			sb.append(" and applyType=");
			sb.append(applyType);
		}
		List l = this.getHibernateTemplate().find(sb.toString());
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return (Long) l.get(0);
		}
	}
	
	/**
	 * 获取ApplyRestaurant对象列表
	 * 
	 * @param applyType
	 * 			int
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			ApplyRestaurant对象列表
	 */
	public List findApplyRestaurants(int applyType,String orderby, int ascOrDesc, final int firstResult, final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from ApplyRestaurant where 1=1");
		if (applyType != -1) {
			sb.append(" and applyType=");
			sb.append(applyType);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
}
